home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / comm / wnos5src.zip / TTYDRIV.C < prev    next >
C/C++ Source or Header  |  1993-10-03  |  5KB  |  250 lines

  1. /* TTY input driver */
  2.  
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include <conio.h>
  6. #include "global.h"
  7. #include "mbuf.h"
  8. #include "session.h"
  9. #include "tty.h"
  10. #include "socket.h"
  11.  
  12. #define    OFF            0
  13. #define    ON            1
  14. #define    CTLB        02        /* use as F3 in dos but no editing */
  15. #define CTLK         0x0b    /* previous session             */
  16. #define CTLL         0x0c    /* next session                 */
  17. #define CTLO         0x0f    /* previous Cstack              */
  18. #define CTLP         0x10    /* next Cstack                  */
  19. #define    CTLU        21        /* delete current line in total */
  20. #define    CTLW        23        /* erase last word including preceding space */
  21. #define    CTLZ        26        /* EOF char in dos */
  22.  
  23. #define DEL            0x7f
  24. #define CLINELEN    162
  25.  
  26. static int  Lastsize = 1;
  27. static char Lastline[CLINELEN + 1] = "\n";
  28.  
  29. static void near
  30. sets(struct session *sp)
  31. {
  32.     sp->tsavex = wherex();
  33.     sp->tsavey = wherey();
  34.     window(1,Nrows-1,80,Nrows);
  35.     gotoxy(sp->bsavex,sp->bsavey);
  36. }
  37.  
  38. static void near
  39. resets(struct session *sp)
  40. {
  41.    sp->bsavex = wherex();
  42.    sp->bsavey = wherey();
  43.    window(1,3,80,Nrows-2);
  44.    gotoxy(sp->tsavex,sp->tsavey);
  45. }
  46.  
  47. static void near
  48. cprintc(struct session *sp,char c)
  49. {
  50.    if(c != CTLZ && sp->ttystate.echo) {
  51.       if(sp->split) {
  52.          sets(sp);
  53.          cprintf("%c_\b",c);
  54.          resets(sp);
  55.       } else {
  56.          putch(c);
  57.       }
  58.    }
  59. }
  60.  
  61. static void near
  62. delchar(struct session *sp)
  63. {
  64.    if(sp->ttystate.echo) {
  65.       if(sp->split) {
  66.          sets(sp);
  67.          cputs(" \b\b_\b");
  68.          resets(sp);
  69.       } else {
  70.          cputs("\b \b");
  71.       }
  72.    }
  73. }
  74.  
  75. /* Accept characters from the incoming tty buffer and process them
  76.  * (if in cooked mode) or just pass them directly (if in raw mode).
  77.  *
  78.  * Echoing (if enabled) is direct to the raw terminal. This requires
  79.  * recording (if enabled) of locally typed info to be done by the session
  80.  * itself so that edited output instead of raw input is recorded.
  81.  * Control-W added by g1emm again.... for word delete.
  82.  * Control-B/Function key 3 added by g1emm for line repeat.
  83.  */
  84. struct mbuf *
  85. ttydriv(struct session *sp,char c)
  86. {
  87.     switch(sp->ttystate.edit){
  88.     case OFF: {
  89.         struct mbuf *bpp = alloc_mbuf(1);
  90.         *bpp->data = c;
  91.         bpp->cnt = 1;
  92.         cprintc(sp,c);
  93.         return bpp;
  94.       }
  95.     case ON: {
  96.         struct mbuf *bp;
  97.         char *cp, *rp = 0;
  98.         int cnt;
  99.  
  100.         if(sp->ttystate.line == NULLBUF) {
  101.             sp->ttystate.line = alloc_mbuf(LINESIZE);
  102.         }
  103.         bp = sp->ttystate.line;
  104.         cp = bp->data + bp->cnt;
  105.  
  106.         /* Perform cooked-mode line editing */
  107.         switch(c) {
  108.         case '\r':    /* CR and LF both terminate the line */
  109.         case '\n':
  110.             *cp = (sp->ttystate.crnl) ? '\n' : c;
  111.  
  112.             if(sp->ttystate.echo) {
  113.                 if(sp->split) {
  114.                     *rp = *cp;
  115.                     *cp = '\0';
  116.                     cputs(bp->data);
  117.                     *cp = *rp;
  118.  
  119.                     if(sp != Trace || bp->cnt) {
  120.                         cputs(Eol);
  121.                     }
  122.                     sets(sp);
  123.                     clrscr();
  124.                     gotoxy(1,1);
  125.                     cputs("_\b");
  126.                     resets(sp);
  127.                 } else {
  128.                     if(Current != Command) {
  129.                         cputs(Eol);
  130.                     } else if(bp->cnt) {
  131.                         cputs(Eol);
  132.                         Command->flag = PPROMPT;
  133.                     }
  134.                 }
  135.             }
  136.             bp->cnt++;
  137.             sp->ttystate.line = NULLBUF;
  138.             Lastsize = bp->cnt;
  139.  
  140.             /* Commandstack by DK5DC*/
  141.             if(bp->cnt > 1) {
  142.                 memcpy(Lastline,bp->data,Lastsize);
  143.                 memcpy(sp->Cs[sp->cont],bp->data,Lastsize);
  144.                 *(sp->Cs[sp->cont] + Lastsize) = '\0';
  145.                 if(++sp->cont >= MAXCS) {
  146.                     sp->cont = 0;
  147.                 }
  148.             }
  149.             /* End Commandstack by Glasi*/
  150.             return bp;
  151.         case '\b':                    /* Backsp->ttystateace */
  152.             if(bp->cnt > 0) {
  153.                 bp->cnt--;
  154.                 delchar(sp);
  155.             }
  156.             break;
  157.         case CTLU:                    /* Line kill */
  158.             while(bp->cnt > 0) {
  159.                 bp->cnt--;
  160.                 delchar(sp);
  161.             }
  162.             break;
  163.         case CTLB:                    /* Use last line to finish current */
  164.             cnt = bp->cnt;            /* save count so far */
  165.             while(bp->cnt > 0) {
  166.                 bp->cnt--;
  167.                 delchar(sp);
  168.             }
  169.             bp->cnt = cnt;
  170. pstr:
  171.             if(bp->cnt < (Lastsize-1)) {
  172.                 memcpy(bp->data+bp->cnt,&Lastline[bp->cnt],(Lastsize-1)-bp->cnt);
  173.                 bp->cnt = Lastsize-1;
  174.             }
  175.             *(bp->data + bp->cnt) = '\0';    /* make it a string */
  176.  
  177.             if(sp->ttystate.echo) {
  178.                 if(sp->split) {
  179.                     sets(sp);
  180.                 }
  181.                 cputs(bp->data);
  182.                 clreol();
  183.  
  184.                 if(sp->split) {
  185.                     cputs("_\b");
  186.                     resets(sp);
  187.                 }
  188.             }
  189.             break ;
  190.         case CTLP:
  191.             if (++sp->cont >= MAXCS || sp->Cs[sp->cont][0] == '\0') {
  192.                 sp->cont = 0;
  193.             }
  194. docs:
  195.             while(bp->cnt > 0) {
  196.                 bp->cnt--;
  197.                 delchar(sp);
  198.             }
  199.             if (sp->Cs[sp->cont][0] != '\0') {
  200.                 strcpy(Lastline,sp->Cs[sp->cont]);
  201.                 Lastsize = strlen(Lastline);
  202.                 bp->cnt = 0;
  203.             }
  204.             goto pstr;
  205.         case CTLO:
  206.             if (sp->Cs[0][0] != '\0') {
  207.                 if (sp->cont > 0) {
  208.                     sp->cont--;
  209.                 } else {
  210.                     sp->cont = MAXCS-1;
  211.                 }
  212.                 while(sp->Cs[sp->cont][0] == '\0') {
  213.                     sp->cont--;
  214.                 }
  215.             }
  216.             goto docs;
  217.         case CTLW:                    /* erase word */
  218.             cnt = 0 ;                /* we haven't seen a printable char yet */
  219.  
  220.             while(bp->cnt > 0) {
  221.                 *(bp->data + bp->cnt--) = '\n';
  222.                 delchar(sp);
  223.  
  224.                 if(isspace((int)*(bp->data + bp->cnt))) {
  225.                     if (cnt) {
  226.                         break;
  227.                     }
  228.                 } else {
  229.                     cnt = 1;
  230.                 }
  231.             }
  232.             break ;
  233.         default:                    /* Ordinary character */
  234.             *cp = c;
  235.  
  236.             if(++bp->cnt >= CLINELEN - 4) {
  237.                 cprintc(sp,'\007');
  238.                 bp->cnt--;
  239.             } else {
  240.                 cprintc(sp,c);
  241.             }
  242.             break;
  243.         }
  244.         break;
  245.       }
  246.     }
  247.     return NULLBUF;
  248. }
  249.  
  250.